<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="tableData">
              <el-table-column type="index" label="序号" width="200" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editDialog(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delData(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :page-size="pageInfo.pagesize" :total="total" layout="prev,pager,next" @current-change="changePage" />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="companyData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="companyData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="companyData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="companyData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </el-card>
      <el-dialog :title="roleForm.id?'编辑':'新增'" :visible="showDialog" @close="btnCancel">
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog :visible="isShowPerm" @close="btnCancelroles">
        <el-tree ref="roleDetails" node-key="id" show-checkbox default-expand-all :check-strictly="true" :data="permList" :props="{label:'name'}" />
        <template #footer type="flex" justify="end">
          <el-button type="primary" size="small" @click="btnOkRole">确定</el-button>
          <el-button size="small" @click="btnCancelroles">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { UpdateRoleApi, getCompanyInfo, getAllroleListApi, delRoleApi, addRoleApi, getRoleDetails, assginRolesForPerm } from '@/api/setting'
import { getAllPermApi } from '@/api/permission'
import { listtoTreeData } from '@/utils'

export default {
  data() {
    return {
      isShowPerm: false,
      showDialog: false,
      companyData: {},
      pageInfo: {
        page: 1,
        pagesize: 2
      },
      total: 0, // 总条数
      tableData: [],
      permList: [],
      roleId: '',
      roleForm: {
        name: '',
        description: ''
      },
      // 数据校验
      rules: {
        name: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { max: 10, message: '长度不能超过 10 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { max: 10, message: '长度不能超过 10 个字符', trigger: 'blur' }
        ] }
    }
  },
  created() {
    this.getCompanyInfo()
    this.getAllroleList()
    this.getAssignPerm()
  },
  methods: {
    async btnOkRole() {
      await assginRolesForPerm({
        id: this.roleId,
        permIds: this.$refs.roleDetails.getCheckedKeys()
      })
      this.isShowPerm = false
      this.$message.success('操作成功')
    },
    btnCancelroles() {
      this.$refs.roleDetails.setCheckedKeys([])
      this.isShowPerm = false
    },
    // 获取分配权限
    async getAssignPerm() {
      this.permList = listtoTreeData(await getAllPermApi(), '0')
    },
    // 弹出分配权限框
    async assignPerm(id) {
      this.roleId = id
      const { permIds } = await getRoleDetails(id)
      this.isShowPerm = true
      this.$nextTick(() => {
        this.$refs.roleDetails.setCheckedKeys(permIds)
      })
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.companyData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
    },
    // 获取所有角色列表
    async getAllroleList() {
      const { rows, total } = await getAllroleListApi(this.pageInfo)
      this.tableData = rows
      this.total = total
    },
    // 切换分页
    changePage(newpage) {
      this.pageInfo.page = newpage
      // 页面渲染
      this.getAllroleList()
    },
    // 删除
    async delData(id) {
      // 提醒用户确认删除数据
      await this.$confirm('请问是否删除？')
      // 请求删除数据
      await delRoleApi(id)
      // 提示删除成功
      this.$message.success('删除成功')
      // 重新渲染回页面
      this.getAllroleList()
    },
    // 关闭弹窗
    async  btnCancel() {
    //  清空数据
      this.roleForm = {
        name: '',
        description: ''
      }
      await this.$refs.roleForm.resetFields()
      // 关闭弹窗
      this.showDialog = false
    },
    async btnOK() {
      // 校验数据
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        //  编辑
        await UpdateRoleApi(this.roleForm)
      } else {
        // 新增
        // 发送请求
        await addRoleApi(this.roleForm)
      }

      // 操作成功提示
      this.$message.success('操作成功')
      // 刷新页面
      this.getAllroleList()
      // 关闭弹窗
      this.showDialog = false
    },
    // 新增
    addRole() {
      // 打开弹窗
      this.showDialog = true
    },
    // 编辑
    async editDialog(id) {
      // 数显回显
      this.roleForm = await getRoleDetails(id)
      // 弹出弹框
      this.showDialog = true
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
